package com.java.yz.redis_jdbc;

import redis.clients.jedis.Jedis;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

//redis的二级索引
//需要提前将商品名称和id存储在Redis中
public class redisApiDemo3 {
    public static void main(String[] args) throws Exception{
        //创建Redis的连接对象
        Jedis jedis = new Jedis("192.168.147.100",6379);
        jedis.select(2);

        //与mysql建立连接对象
        Connection conn = MySQLTool.getPreparedStatement();

        //查数据
        Statement statement = conn.createStatement();
//        ResultSet resultSet = statement.executeQuery("select id,name from jd_goods");
//
//        while (resultSet.next()){
//            String string1 = resultSet.getString(1);
//            String string2 = resultSet.getString(2);
//
//            //name作为key id作为value
//            jedis.set(string2,string1);
//        }

        //开始查询数据   [通过商品名称来获取商品编号，再根据商品编号来mysql中查数据]
        //输入电脑的一些信息，他就会返回该电脑的型号
        String id = jedis.get("华硕无畏15 2023 13代酷睿i5标压 15.6英寸轻薄便携大屏高性能办公笔记本电脑(i5-13500H 16G 512G 护眼屏)银");

        //拿着id去主数据库中查找
        PreparedStatement preparedStatement = conn.prepareStatement("select * from jd_goods where id=?");
        preparedStatement.setString(1,id);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            String ids = resultSet.getString(1);
            String name = resultSet.getString(2);
            String price = resultSet.getString(3);
            String shop = resultSet.getString(4);
            String flags= resultSet.getString(5);
            String area = resultSet.getString(6);
            String cpu = resultSet.getString(7);
            String size = resultSet.getString(8);
            String me = resultSet.getString(9);
            String gup = resultSet.getString(10);
            String comments = resultSet.getString(11);
            System.out.println(name+" | "+price+" | "+shop+" | "+flags+" | "+area+" | "+cpu+" | "+size+" | "+me+" | "+gup+" | "+comments);
        }

    }
}
